<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\admin\validate\AdminValidate;
use app\domain\admin\LoginDomain;
use think\Exception;
use think\response\Json;

class Login extends Common
{
    public function login(LoginDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(AdminValidate::class);
            if (!$validate->scene('login')->check($params)) {
                throw new Exception($validate->getError());
            }

            $result = $domain->login($params['username'], $params['password']);
            return jsonReturn((bool)$result, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }

    public function getUserInfo(LoginDomain $domain): Json
    {
        try {
            $result = $domain->getUserInfo();
            return jsonReturn((bool)$result, $domain->errMsg, $result);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }

    public function logout(LoginDomain $domain): Json
    {
        try {
            $result = $domain->logout();
            return jsonReturn(true, $domain->errMsg, $result);
        } catch (\Exception $e) {
            return jsonReturn(false, lang($e->getMessage()));
        }
    }
}
